<?php

namespace app\model\v1\gift;

use app\model\order\StoreOrder;
use app\model\user\User;
use app\services\order\StoreOrderCreateServices;
use app\services\user\UserAddressServices;
use crmeb\traits\ModelTrait;
use think\Exception;
use think\facade\Db;
use think\Model;


class GiftShareInfo extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];

    public function user()
    {
        return $this->hasOne(User::class,'uid','uid');
    }

    public function carduser()
    {
        return $this->hasOne(GiftCardUser::class,'id','card_user_id');
    }


    /**
     * 分享礼品卡-自定义信息
     * @param $param
     * @return string
     */
    public static function createCardShareInfo($param = [])
    {
        $find = self::whereNull("delete_time")->where("card_user_id", $param['card_user_id'])->find();
        if(!empty($find)) Throw new Exception("此卡已分享出去了！");

        $card_find = GiftCardUser::where("id", $param['card_user_id'])->where("status", 10)->find();
        if(!empty($card_find)) Throw new Exception("此卡已制作实体卡不能分享！");

        if(!empty($param['video'])){
            $http = substr($param["video"],0,4);
            if($http != "http") Throw new Exception("video参数不正确！");
        }

        $add = [
            "uid" => $param['uid'],
            "card_user_id" => $param['card_user_id'],
            "bless" => $param['bless'] ?? '',
            "voice" => $param['voice'] ?? '',
            "video" => $param['video'] ?? '',
            "effect" => $param['effect'] ?? '',
            "create_time" => time(),
        ];

        GiftCardUser::where("id", $param['card_user_id'])->update(["type" => 3]);

        return self::insert($add);
    }




    public static function getCardShareInfo($param = [])
    {
        $find = self::field("*")
            ->with([
                'carduser' => function ($query) {
                    $query->field('*');
                },
                'user' => function ($query) {
                    $query->field('uid,nickname,phone,real_name,avatar');
                }
            ])
            ->whereNull("delete_time")
            ->where(["card_user_id" => $param['card_user_id'], "type"=>1])
            ->find();
        return $find;
    }


    /**
     * 购买礼品卡-保存编辑祝福语
     * @param $param
     * @return mixed
     */
    public static function saveGiftShareInfo($param = [])
    {
        if(empty($param["id"])){   //添加
            if(!empty($param['video'])){
                $http = substr($param["video"],0,4);
                if($http != "http") Throw new Exception("video参数不正确！");
            }

            $add = [
                "uid" => $param['uid'],
                "card_user_id" => $param['card_id'],
                "bless" => $param['bless'] ?? '',
                "voice" => $param['voice'] ?? '',
                "video" => $param['video'] ?? '',
                "effect" => $param['effect'] ?? '',
                "type" => 2,
                "create_time" => time(),
            ];
            return self::insertGetId($add);

        }else{

            $update["update_time"] = time();   //更新时间
            if(!empty($param['user_id']))       $update["uid"] = $param['uid']; //用户id
            if(!empty($param['card_id']))       $update["card_user_id"] = $param['card_id'];
            if(!empty($param['bless']))         $update["bless"] = $param['bless'];
            if(!empty($param['voice']))         $update["voice"] = $param['voice'];
            if(!empty($param['video']))         $update["video"] = $param['video'];
            if(!empty($param['effect']))        $update["effect"] = $param['effect'];

            return self::where("id", $param['id'])->update($update);
        }
    }


    public static function getGiftShareInfo($param = [])
    {
        $find = self::field("*")
            ->with([
                'carduser' => function ($query) {
                    $query->field('*');
                },
                'user' => function ($query) {
                    $query->field('uid,nickname,phone,real_name');
                }
            ])
            ->whereNull("delete_time")
            ->where(function ($query) use($param){
                if(!empty($param['card_id'])) $query->where("card_user_id", $param['card_id']);
                if(!empty($param['order_id'])) $query->where("order_id", $param['order_id']);
            })
            ->where(["uid" => $param['uid'], "type" => 2])
            ->find();
        return $find;
    }



    public static function associationGiftOrderId($param = [])
    {
        $update["update_time"] = time();   //更新时间
        if(!empty($param['order_id']))        $update["order_id"] = $param['order_id'];

        return self::where("id", $param['share_info_id'])->update($update);
    }





}